﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using EZCRS.HCMS.SqlClient;
using Newtonsoft.Json;
using EZCRS.HCMS.Entity;

namespace EZCRS.HCMS.Bll.AIR
{
    public class BllBookLogView
    {
        public DataSet GetBookLog(string jsonSearch, int pageindex, string orders)
        {
            StringBuilder SqlSearch = new StringBuilder();
           

            Newtonsoft.Json.Linq.JObject obj = (Newtonsoft.Json.Linq.JObject)JavaScriptConvert.DeserializeObject(jsonSearch);

        //    sstr+="Confnum:\""+$F("txtConfnum").value+"\",";
        //sstr+="pms:\""+$F("txtpms").value+"\",";
        //sstr+="iata:\""+$F("iata").value+"\",";
        //sstr+="UserName:\""+$F("txtUserName").value+"\",";
        //sstr+="txtiata:\""+$F("txtiata").value+"\",";
        //sstr+="guestname:\""+$F("guestname").value+"\",";
        //sstr+="txtAddress:\""+$F("txtAddress").value+"\",";
        //sstr+="txtPhone:\""+$F("txtPhone").value+"\",";
        //tempdate=$FDate("txtBookStartDate");
        //    sstr+="txtRZDate:\""+$FDate("txtRZDate")+"\",";
        //sstr+="txtLDDate:\""+$FDate("txtLDDate")+"\",";
        //sstr+="selPmsHotel:\""+$F("selPmsHotel").value+"\",";
        //sstr+="txtRemark:\""+$F("txtRemark").value+"\",";
        //sstr+="paystatus:\""+$F("drppaystatus").value+"\",";
        //sstr+="paymethod:\""+$F("drppaymethod").value+"\",";
          
            // sstr+="BookStatus:\""+$FN("bookstatus")+"\",";
        //sstr+="BookGetStatus:\""+$FN("bookgetstatus")+"\",";
        //    sstr+="CheckinMI:\""+$FDate("txtcheckinstart")+"|"+$FDate("txtcheckinend")+"\",";
        //sstr+="CheckOutMI:\""+$FDate("txtcheckoutstart")+"|"+$FDate("txtcheckoutend")+"\",";

            HttpCookie co1 = null;
            try
            {
                co1 = System.Web.HttpContext.Current.Request.Cookies["cms"];
            }
            catch { }
            if (co1 != null && co1.Value != "")
            {
                int propertyid = (co1.Values["p"] != "") ? Convert.ToInt32(co1.Values["p"]) : 0;
                if (propertyid > 0)
                {
                    SqlSearch.Append(" and property_id=" + propertyid.ToString() + " ");
                }
            }

            string strTemp = obj.Value<string>("BookStatus");
            if (strTemp != "")
            {
                string[] str=strTemp.Split(',');
                string strB = "";
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] != "")
                    {
                        strB+=" or status='" + str[i] + "' ";
                    }
                }
                if (strB.Length > 0)
                {
                    strB = strB.Substring(4);
                    SqlSearch.Append(" and ("+strB+")");
                }
            }

            strTemp = obj.Value<string>("CheckinMI");
            if (strTemp != "")
            {
                string[] str = strTemp.Split('|');
                string strB = "";
                if (str[0] != "")
                {
                    strB += " and indate>'"+str[0]+"'";
                }
                if (str[1] != "")
                {
                    strB += " and indate<'"+str[1]+" 23:59:59'";
                }
                if (strB.Length > 0)
                {
                    strB = strB.Substring(4);
                    SqlSearch.Append(" and " + strB + "");
                }
            }

            strTemp = obj.Value<string>("CheckOutMI");
            if (strTemp != "")
            {
                string[] str = strTemp.Split('|');
                string strB = "";
                if (str[0] != "")
                {
                    strB += " and outdate>'" + str[0] + "'";
                }
                if (str[1] != "")
                {
                    strB += " and outdate<'" + str[1] + " 23:59:59'";
                }
                if (strB.Length > 0)
                {
                    strB = strB.Substring(4);
                    SqlSearch.Append(" and " + strB + "");
                }
            }

            strTemp = obj.Value<string>("BookGetStatus");
            if (strTemp != "")
            {
                string[] str = strTemp.Split(',');
                string strB = "";
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] != "")
                    {
                        strB += " or getstatus=" + str[i] + " ";
                    }
                }
                if (strB.Length > 0)
                {
                    strB = strB.Substring(4);
                    SqlSearch.Append(" and (" + strB + ")");
                }
            }

            strTemp = obj.Value<string>("Confnum");
            if (strTemp != "")
            {
                SqlSearch.Append(" and confnum='" + strTemp + "' ");
            }

            strTemp = obj.Value<string>("pms");
            if (strTemp != "")
            {
                SqlSearch.Append(" and pmsnum='"+strTemp+"' ");
            }

            strTemp = obj.Value<string>("iata");
            if (strTemp != "")
            {
                SqlSearch.Append(" and iata='"+strTemp+"' ");
            }

            //strTemp = obj.Value<string>("UserName");
            //if (strTemp != "")
            //{
            //    SqlSearch.Append(" and userID="+strTemp+"");
            //}

            //strTemp = obj.Value<string>("txtiata");
            //if (strTemp != "")
            //{
            //    SqlSearch.Append("");
            //}

            strTemp = obj.Value<string>("guestname");
            if (strTemp != "")
            {
                SqlSearch.Append(" and guestfirstname+' '+guestlastname like '%"+strTemp+"%'");
            }

            strTemp = obj.Value<string>("txtAddress");
            if (strTemp != "")
            {
                SqlSearch.Append(" and guestaddress like '%"+strTemp+"%'");
            }

            strTemp = obj.Value<string>("txtPhone");
            if (strTemp != "")
            {
                SqlSearch.Append(" and guestphone like '%"+strTemp+"%'");
            }

            strTemp = obj.Value<string>("selPmsHotel");
            if (strTemp != "-1")
            {
                SqlSearch.Append(" pms='"+strTemp+"'");
            }

            strTemp = obj.Value<string>("txtRemark");
            if (strTemp != "")
            {
                SqlSearch.Append(" and remarks like '%"+strTemp+"%'");
            }

            //strTemp = obj.Value<string>("paystatus");
            //if (strTemp != "")
            //{
            //    SqlSearch.Append(" and pay="+strTemp+" ");
            //}

            strTemp = obj.Value<string>("paymethod");
            if (strTemp != "-1")
            {
                SqlSearch.Append(" and pay=" + strTemp + " ");
            }

            strTemp = obj.Value<string>("txtRZDate");
            if (strTemp != "")
            {
                SqlSearch.Append(" and convert(char(10),indate,20)='"+strTemp+"'");
            }

            if (SqlSearch.Length > 5)
            {
                SqlSearch.Remove(0,4);
            }
           

            return new Sql_BookLogView().GetBookLog(" * ", SqlSearch.ToString(), orders, pageindex);
        }

        public DataRow GetBookLog(int id)
        {
            return new Sql_BookLogView().GetBookLog(id);
        }

        public bool UpdateStatus(string bookstatus, string bookgetstatus,string RoomTotal,string RoomCode,string RateCode,string StartDate,string EndDate, int id,int pid)
        {
            bool returnBool = true;
            //修改日志
            returnBool=new Sql_BookLogView().UpdateStatus(bookstatus, bookgetstatus, id);

            //房量控制
            if (bookgetstatus == "4")
            {

                List<string[]> list = new List<string[]>();

                DateTime dateStart = Convert.ToDateTime(StartDate);
                DateTime dateEnd = Convert.ToDateTime(EndDate);

                DateTime dateTemp=dateStart;
                while (dateTemp.ToShortDateString() != dateEnd.ToShortDateString() && dateTemp<dateEnd)
                {
                    list.Add(new string[] { dateTemp.ToShortDateString(),RoomTotal});
                    dateTemp=dateTemp.AddDays(1);
                }

                Bll.AIR.AIR air = new EZCRS.HCMS.Bll.AIR.AIR();
                if (!air.ReturnRoom(pid, RoomCode, RateCode, list))
                {
                    returnBool = false;
                }
            }

            return returnBool;
        }

        /// <summary>
        /// 取消订单，归还放量
        /// </summary>
        /// <param name="verifyCode"></param>
        public void CancelBooklog(string verifyCode)
        {
            List<string[]> roomDay = new List<string[]>();
            DataSet ds = new Sql_BookLogView().GetBookLog("*", " verifycode='" + verifyCode + "'", "", 1);
            if (ds.Tables[0].Rows.Count > 0)
            {
                int pId=Convert.ToInt32(ds.Tables[0].Rows[0]["property_id"].ToString());
                string roomCode=ds.Tables[0].Rows[0]["roomtype"].ToString();
                string rateCode=ds.Tables[0].Rows[0]["rateclass"].ToString();
                string inDate=ds.Tables[0].Rows[0]["indate"].ToString();
                string outDate=ds.Tables[0].Rows[0]["outdate"].ToString();
                string roomTotal = ds.Tables[0].Rows[0]["rooms"].ToString();

                DateTime dtStart = Convert.ToDateTime(inDate);
                DateTime dtEnd = Convert.ToDateTime(outDate);

                DateTime dtTemp=dtStart;
                while (dtTemp < dtEnd)
                {
                    roomDay.Add(new string[]{dtTemp.ToShortDateString(),roomTotal});
                }

                new EZCRS.HCMS.Bll.AIR.AIR().ReturnRoom(pId, roomCode, rateCode, roomDay);
            }
        }
    }
}
